package com.gdth.bank.ui;

import java.util.ArrayList;
import java.util.List;

import org.greenrobot.eventbus.EventBus;

import com.gdth.bank.BankApplication;
import com.gdth.bank.R;
import com.gdth.bank.adapter.NotificationAdapter;
import com.gdth.bank.event.MessageEvent;
import com.gdth.bank.presenter.NotificationPresenter;
import com.gdth.bank.utils.AppUtil;
import com.gdth.bank.utils.SharedPreferencesUtil;
import com.gdth.bank.view.NotificationView;
import com.gdth.core.annotation.view.ViewInject;
import com.gdth.core.widget.BaseFrameLayout;

import com.gdth.database.bean.NotificationT;
import com.gdth.database.bean.TAppYh;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;


/**
 * @author Administrator
 *
 */
public class NotificationFragment extends BaseFrameLayout implements NotificationView {
	
    @ViewInject(id = R.id.notification)
    Button notification;
    
    @ViewInject(id = R.id.notificationList)
	ListView listView;
    
    NotificationAdapter adapter ;
    private List<NotificationT> listData = new ArrayList();
    private NotificationPresenter persenter;
    private TAppYh mTAppYh;
    public int showStatu = 1 ; 			// 1.所有， 2.已读  3.未读， 4.搜索 5.删除 = 
	public NotificationFragment(Context context, int layout) {
		super(context, R.layout.fragment_notification);
		// TODO Auto-generated constructor stub
	}
	
	public void refreshData() {
		 mainHandler.post(new Runnable() {
		        @Override
		        public void run() {
					adapter.setRefresh(listData);
		        }
		    });
	}
	
	
   Handler mainHandler = new Handler(Looper.getMainLooper());
	   

    @Override
    public void initializeView() {
    	persenter = new NotificationPresenter(this);
    	mTAppYh = BankApplication.mInstance.getCurrentUser();
    	adapter = new NotificationAdapter(getContext(),listData,persenter,mTAppYh.getSfz());
    	listView.setAdapter(adapter);
    	
    	onGetLocalData(true);		// 获取本地数据 
    	
    	//  获取网路数据
    	notification.setOnClickListener(new OnClickListener(){  
            
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
//				     
			}
        });
    }
    // 获取所有数据
    public void  onGetLocalData(boolean getNet) {
    	if(showStatu == 2) {
    		persenter.getNoReadList(mTAppYh.getSfz());		// 获取本地数据 
    	}else if(showStatu == 3){
    		persenter.getReadList(mTAppYh.getSfz());		// 获取本地数据 
    	}else {
    		persenter.getAllList(mTAppYh.getSfz(),getNet);		// 获取本地数据 
    	}
    	
    }
    // 获取已读数据
    public void onGetReadData() {
    	persenter.getReadList(mTAppYh.getSfz());		// 获取本地数据 
    }
    // 获取未读数据
    public void onGetNoReadData() {
    	persenter.getNoReadList(mTAppYh.getSfz());		// 获取本地数据 
    }
    // 搜索其他数据
    public void OnGetOtherTypeData(int type,String content) {
//    	switch(type) {
//    	case 1:
//    		persenter.getTitleList(mTAppYh.getSfz(), content);		// 获取本地数据 
//    		break;
//    	case 2:
//    		persenter.getContentList(mTAppYh.getSfz(), content);		// 获取本地数据 
//    		break;
//    		
//    	}
    	persenter.getTitleList(mTAppYh.getSfz(), content);		// 获取本地数据 
    	
    }
    // 删除更多数据
    public void  OnDelMoreData() {
    	adapter.OnSelDel(true);
    }
    
    // 确定删除
    public void OnSureDelMoreData() {
    	adapter.OnSelDel(false);
    	adapter.delNotification();
    }
    
    public void  delNotification(String xxid) {
    	List<NotificationT> selData = new ArrayList<NotificationT>();
    	selData.addAll(adapter.getSelListData());
    	adapter.getSelListData().clear();
    	persenter.delLocalNotification(selData);
    }
    
  

    public void readNotification(String xxid) {
    	
//    	int readId = Integer.parseInt(xxid);
//    	for(int i = 0 ; i < listData.size() ; i ++) {
//    		if(readId ==listData.get(i).getXxtsId() ) {
//    			listData.remove(i);
//    		}
//    	}
//    	getAllNotificationList(listData);
    	persenter.changerLocalNotification(xxid);
    }

	@Override
	public void getAllNotificationList(List<NotificationT> mlist) {			// 返回本地数据
		if(mlist != null && mlist.size() > 0) {			// 本地有数据的情况
			if(showStatu ==1)
			SharedPreferencesUtil.saveSharePreference(AppUtil.NOTIFICATION, AppUtil.LAST_NOTIFICATION_ID, String.valueOf(mlist.get(0).getXxtsId()));
		//	SharedPreferManager.put(BankApplication.mInstance.getBaseContext(), AppUtil.NOTIFICATION, key, value);
			listData.clear();
			listData.addAll(mlist);
			refreshData();
			int  number = 0;
			for(int i = 0; i < mlist.size(); i++) {
				if(mlist.get(i).getXxzt() == 1) {
					number ++;
				}
			}
			EventBus.getDefault().post(new MessageEvent(MessageEvent.CHANGE_NOTIFICATION_NUMBER,String.valueOf(number)));
		}else {
			EventBus.getDefault().post(new MessageEvent(MessageEvent.CHANGE_NOTIFICATION_NUMBER,String.valueOf(0)));
		}
	}

	@Override
	public void upDataNotificationList(List<NotificationT> mlist) {			// 更新本地数据
	
	}

	@Override
	public void showProgressDialog(String msg) {
		
		
	}

	@Override
	public void hideProgressDialog() {
	
		
	}

	@Override
	public void showMessage(String msg) {
		
		
	}
	

	@Override
	public void getNetList(List<NotificationT> mlist) {		// 获取到网路新数据 --- 在服务中获取到的数据进行弹窗提示
		// TODO Auto-generated method stub

	}

	public int getShowStatu() {
		return showStatu;
	}

	public void setShowStatu(int showStatu) {
		this.showStatu = showStatu;
	}
	
	

}
